<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of RevisorPaperDAO
 *
 * @author cshepherd
 */
class RevisorPaperDAO extends DAO {
    public function select($fields='*', $condition='TRUE', $page='1', $rows_per_page='20', $order_by='id', $order_by_type='DESC') {
        $list_revisor_paper = array();
        $data_revisor_paper = array();
        try {
            $rows = 0;
            $start = intval(($page-1)*$rows_per_page);
            $query = "SELECT COUNT(id) AS rows FROM revisor_paper";
            $query .= " WHERE ".$condition;
            $stmt = $this->dbh->query($query);
            $rows = $stmt->fetchObject()->rows;
            $query = 'SELECT '. $fields .' FROM revisor_paper';
            $query .= ' WHERE '.$condition.' ';
            $pages = ceil($rows/$rows_per_page);
            $query .= 'ORDER BY '.$order_by;
            $query .= ' '.$order_by_type;
            $stmt->closeCursor();
            $query .= ' LIMIT '.$start.",".$rows_per_page;
            $stmt = $this->dbh->prepare($query);
            /*$stmt->bindParam(':start', $start, PDO::PARAM_INT);
            $stmt->bindParam(':rows_per_page', $rows_per_page, PDO::PARAM_INT);
            $stmt->bindParam(':order_by', $order_by);
            $stmt->bindParam(':order_by_type', $order_by_type);*/
            $stmt->setFetchMode(PDO::FETCH_OBJ);
            $stmt->execute();
            $result = $stmt->fetchAll();
            foreach($result as $row) {
                $objRevisorPaperTO = new RevisorPaperTO;
                $objRevisorPaperTO->setIdRevisorPaper($row->id);
                $objRevisorPaperTO->setIdRevisor($row->id_revisor);
                $objRevisorPaperTO->setIdPaper($row->id_paper);
                $objRevisorPaperTO->setCalificacion($row->calificacion);
                $objRevisorPaperTO->setEvaluacion($row->evaluacion);
                $list_revisor_paper[count($list_revisor_paper)+1] = $objRevisorPaperTO;
            }
            $data_revisor_paper['list_revisor_paper']=$list_revisor_paper;
            $data_revisor_paper['pages']=$pages;
            $data_revisor_paper['rows']=$rows;
            return $data_revisor_paper;

        }catch(Exception $e) {
            throw $e;
        }
    }

    protected function insert($Entidad) {
        try {
            $id_revisor = $Entidad->getIdRevisor();
            $id_paper = $Entidad->getIdPaper();


            $query = 'INSERT INTO revisor_paper (id_revisor, id_paper) ';
            $query .= 'VALUES(:id_revisor, :id_paper)';
            $stmt = $this->dbh->prepare($query);
            $stmt->bindParam(':id_revisor', $id_revisor);
            $stmt->bindParam(':id_paper', $id_paper);
            $stmt->setFetchMode(PDO::FETCH_OBJ);
            if($stmt->execute()) {
                return $this->dbh->lastInsertId();
            }
            else {
                return FALSE;
            }
        }
        catch(Exception $e) {
            throw $e;
        }
    }

    public function load($objRevisorPaperTO) {
        try {
            $query = 'SELECT * FROM revisor_paper WHERE id_paper = :id_paper AND id_revisor = :id_revisor LIMIT 1';
            $stmt = $this->dbh->prepare($query);
            $stmt->bindParam(':id_paper', $objRevisorPaperTO->getIdPaper());
            $stmt->bindParam(':id_revisor', $objRevisorPaperTO->getIdRevisor());
            $stmt->setFetchMode(PDO::FETCH_OBJ);
            $stmt->execute();
            $resultado = $stmt->fetch();
            $objRevisorPaperTO->setIdRevisorPaper($resultado->id);
            $objRevisorPaperTO->setIdRevisor($resultado->id_revisor);
            $objRevisorPaperTO->setIdPaper($resultado->id_paper);
            $objRevisorPaperTO->setCalificacion($resultado->calificacion);
            $objRevisorPaperTO->setEvaluacion($resultado->evaluacion);
            return $objRevisorPaperTO;
        }
        catch(Exception $e) {
            throw $e;
        }
    }

    public function delete($id) {
        return "";
    }

    protected function update($objetoTO) {
        try{
            $id_paper = $objetoTO->getIdPaper();
            $id_revisor = $objetoTO->getIdRevisor();
            $calificacion = $objetoTO->getCalificacion();
            $evaluacion = $objetoTO->getEvaluacion();
            $query = 'UPDATE revisor_paper';
            $query .= ' SET calificacion = :calificacion, evaluacion = :evaluacion';
            $query .= ' WHERE id_revisor = :id_revisor AND id_paper = :id_paper';
            /*echo $query;
            echo "UPDATE revisor_paper SET calificacion = 11, evaluacion = 'hola' WHERE id_revisor = 35 AND id_paper = 4";*/
            $stmt = $this->dbh->prepare($query);
            $stmt->bindParam(':calificacion',$calificacion);
            $stmt->bindParam(':evaluacion',$evaluacion);
            $stmt->bindParam(':id_revisor',$id_revisor, PDO::PARAM_INT);
            $stmt->bindParam(':id_paper',$id_paper, PDO::PARAM_INT);
            $stmt->setFetchMode(PDO::FETCH_OBJ);
            return $stmt->execute();
        }catch (Exception $e){
            throw $e;
        }
    }
}
?>
